package com.lin.gulimallcart.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.lin.gulimall.common.constant.CartConstant;
import com.lin.gulimall.common.utils.R;
import com.lin.gulimallcart.exception.CartExceptionHandler;
import com.lin.gulimallcart.feign.ProductFeignService;
import com.lin.gulimallcart.interceptor.CartInterceptor;
import com.lin.gulimallcart.service.CartService;
import com.lin.gulimallcart.to.UserInfoTo;
import com.lin.gulimallcart.vo.CartItemVo;
import com.lin.gulimallcart.vo.CartVo;
import com.lin.gulimallcart.vo.SkuInfoVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.BoundHashOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.stream.Collectors;

@Service
public class CartServiceImpl implements CartService {

    @Autowired
    private StringRedisTemplate redisTemplate;
    @Autowired
    private ProductFeignService feignService;
    @Autowired
    private ThreadPoolExecutor executor;
    @Autowired
    private ProductFeignService productFeignService;


    @Override
    public CartVo getCart() throws ExecutionException, InterruptedException {
        CartVo cartVo = new CartVo();
        UserInfoTo userInfoTo = CartInterceptor.toThreadLocal.get();
        if (userInfoTo.getUserId() != null) {
            // 已登录
            String cartKey = CartConstant.CART_PREFIX + userInfoTo.getUserId();
            // 临时购物车的键
            String temptCartKey = CartConstant.CART_PREFIX + userInfoTo.getUserKey();

            //2、如果临时购物车的数据还未进行合并
            List<CartItemVo> tempCartItems = getCartItems(temptCartKey);
            if (tempCartItems != null) {
                //临时购物车有数据需要进行合并操作
                for (CartItemVo cartItemVo : tempCartItems) {
                    addToCart(cartItemVo.getSkuId(),cartItemVo.getCount());
                }
                //清除临时购物车的数据
                clearCartInfo(temptCartKey);
            }

            //3、获取登录后的购物车数据【包含合并过来的临时购物车的数据和登录后购物车的数据】
            List<CartItemVo> cartItemVoList = getCartItems(cartKey);
            cartVo.setItems(cartItemVoList);
        } else {
            //没登录
            String temptCartKey = CartConstant.CART_PREFIX + userInfoTo.getUserKey();
            //获取临时购物车里面的所有购物项
            List<CartItemVo> cartItemVoList = getCartItems(temptCartKey);
            cartVo.setItems(cartItemVoList);
        }
        return cartVo;
    }

    private void clearCartInfo(String temptCartKey) {
        redisTemplate.delete(temptCartKey);
    }

    public CartItemVo addToCart(Long skuId, Integer num) throws ExecutionException, InterruptedException {

        //拿到要操作的购物车信息
        BoundHashOperations<String, Object, Object> cartOps = getCartOps();

        //判断Redis是否有该商品的信息
        String productRedisValue = (String) cartOps.get(skuId.toString());
        //如果没有就添加数据
        if (StringUtils.isEmpty(productRedisValue)) {
            //2、添加新的商品到购物车(redis)
            CartItemVo cartItemVo = new CartItemVo();
            //开启第一个异步任务
            CompletableFuture<Void> getSkuInfoFuture = CompletableFuture.runAsync(() -> {
                //1、远程查询当前要添加商品的信息
                R productSkuInfo = feignService.getInfo(skuId);
                SkuInfoVo skuInfoVo = productSkuInfo.getData("skuInfo",new TypeReference<SkuInfoVo>() {});
                //数据赋值操作
                cartItemVo.setCount(num);
                cartItemVo.setSkuId(skuInfoVo.getSkuId());
                cartItemVo.setPrice(skuInfoVo.getPrice());
                cartItemVo.setImage(skuInfoVo.getSkuDefaultImg());
                cartItemVo.setTitle(skuInfoVo.getSkuTitle());
            },executor);

            //开启第二个异步任务
            CompletableFuture<Void> getSkuAttrValuesFuture = CompletableFuture.runAsync(() -> {
                //2、远程查询skuAttrValues组合信息
                List<String> skuSaleAttrValues = feignService.getSkuSaleAttrValues(skuId);
                cartItemVo.setSkuAttrValues(skuSaleAttrValues);
            },executor);

            //等待所有的异步任务全部完成
            CompletableFuture.allOf(getSkuInfoFuture,getSkuAttrValuesFuture).get();
            String cartItemJson = JSON.toJSONString(cartItemVo);
            cartOps.put(skuId.toString(), cartItemJson);

            return cartItemVo;
        } else {
            //购物车有此商品，修改数量即可
            CartItemVo cartItemVo = JSON.parseObject(productRedisValue, CartItemVo.class);
            cartItemVo.setCount(cartItemVo.getCount() + num);
            //修改redis的数据
            String cartItemJson = JSON.toJSONString(cartItemVo);
            cartOps.put(skuId.toString(), cartItemJson);

            return cartItemVo;
        }
    }

    @Override
    public CartItemVo getCartItem(Long skuId) {
        //拿到要操作的购物车信息
        BoundHashOperations<String, Object, Object> cartOps = getCartOps();

        String redisValue = (String) cartOps.get(skuId.toString());

        return JSON.parseObject(redisValue, CartItemVo.class);
    }

    @Override
    public void checkItem(Long skuId, Integer checked) {
        //查询购物车里面的商品
        CartItemVo cartItem = getCartItem(skuId);
        //修改商品状态
        cartItem.setCheck(checked == 1 ? true:false);

        //序列化存入redis中
        String redisValue = JSON.toJSONString(cartItem);
        BoundHashOperations<String, Object, Object> cartOps = getCartOps();
        cartOps.put(skuId.toString(), redisValue);
    }

    @Override
    public void changeItemCount(Long skuId, Integer num) {
        //查询购物车里面的商品
        CartItemVo cartItem = getCartItem(skuId);
        //修改商品状态
        cartItem.setCount(num);

        //序列化存入redis中
        String redisValue = JSON.toJSONString(cartItem);
        BoundHashOperations<String, Object, Object> cartOps = getCartOps();
        cartOps.put(skuId.toString(), redisValue);
    }

    @Override
    public void deleteIdCartInfo(Integer skuId) {
        BoundHashOperations<String, Object, Object> cartOps = getCartOps();
        cartOps.delete(skuId.toString());
    }

    @Override
    public List<CartItemVo> getUserCartItems() {
        List<CartItemVo> cartItemVoList = new ArrayList<>();
        //获取当前用户登录的信息
        UserInfoTo userInfoTo = CartInterceptor.toThreadLocal.get();
        //如果用户未登录直接返回null
        if (userInfoTo == null) {
            return null;
        } else {
            //获取购物车项
            String cartKey = CartConstant.CART_PREFIX + userInfoTo.getUserId();
            //获取所有的信息
            List<CartItemVo> cartItems = getCartItems(cartKey);
            if (cartItems == null) {
                throw new CartExceptionHandler();
            }
            //筛选出选中的
            cartItemVoList = cartItems.stream().filter(item -> {
                return item.getCheck();
            }).map(item -> {
                //更新为最新的价格（查询数据库）
                BigDecimal price = productFeignService.getPrice(item.getSkuId());
                item.setPrice(price);
                return item;
            }).collect(Collectors.toList());
        }
        return cartItemVoList;
    }

    /**
     * 获取到我们要操作的购物车
     * @return
     */
    private BoundHashOperations<String, Object, Object> getCartOps() {
        //先得到当前用户信息
        UserInfoTo userInfoTo = CartInterceptor.toThreadLocal.get();

        String cartKey = "";
        if (userInfoTo.getUserId() != null) {
            // 已登录
            cartKey = CartConstant.CART_PREFIX + userInfoTo.getUserId();
        } else {
            // 未登录
            cartKey = CartConstant.CART_PREFIX + userInfoTo.getUserKey();
        }

        //绑定指定的key操作Redis
        return redisTemplate.boundHashOps(cartKey);
    }

    private List<CartItemVo> getCartItems(String temptCartKey) {
        //获取购物车里面的所有商品
        BoundHashOperations<String,Object,Object> operations = redisTemplate.boundHashOps(temptCartKey);
        List<Object> values = operations.values();
        if (values != null && values.size() > 0) {
            List<CartItemVo> cartItemVoStream = values.stream().map(obj -> {
                String str = (String) obj;
                CartItemVo cartItemVo = JSON.parseObject(str, CartItemVo.class);
                return cartItemVo;
            }).collect(Collectors.toList());
            return cartItemVoStream;
        }
        return null;
    }
}
